//
//  AnimationCollectViewController.m
//  Double
//
//  Created by dllo on 15/5/25.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "AnimationCollectViewController.h"
#import "AnimationCollectDatabase.h"
#import "AnimationCollectModel.h"
#import "DB.h"
#import "CartoonRecommendDetailViewController.h"


@interface AnimationCollectViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation AnimationCollectViewController

- (void)dealloc
{
    [_tableArray release];
    [_tableView release];
    [super dealloc];
}
- (void)awakeFromNib
{
    [super awakeFromNib];
    self.tableArray = [NSMutableArray array];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createView];
    
    
    // Do any additional setup after loading the view.
}
#pragma mark 创建TableView
- (void)createView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    [self.tableView release];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    NSArray *array = [AnimationCollectDatabase selectAll];
//    self.tableArray = [AnimationCollectDatabase selectAll];
    [self.tableArray addObjectsFromArray:array];
    return self.tableArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    static NSString * cellInde = @"cell";
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellInde];
    
    if (!cell) {
        
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellInde]autorelease];
        
    }
    
    NSArray *array = [AnimationCollectDatabase selectAll];
    [self.tableArray addObjectsFromArray:array];
    
    AnimationCollectModel *model1 = [self.tableArray objectAtIndex:indexPath.row];
    
    NSString *AnimationName = model1.AnimationName;
    
    cell.textLabel.text = AnimationName;
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    CartoonRecommendDetailViewController *cartoon = [[CartoonRecommendDetailViewController alloc] init];
    
//    self.tableArray = [AnimationCollectDatabase selectAll];
    NSArray *array = [AnimationCollectDatabase selectAll];
    [self.tableArray addObjectsFromArray:array];
    
    AnimationCollectModel *model1 = [self.tableArray objectAtIndex:indexPath.row];
    
    cartoon.iD = [NSString stringWithFormat:@"%ld", model1.AnimationId];
    
    cartoon.titleName = model1.AnimationName;

    
    
    [self.navigationController pushViewController:cartoon animated:YES];
    [cartoon release];
    
    
}


// 重写系统按钮触发的方法(让其处于编辑状态)
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}

// 编辑删除和插入
-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.tableArray.count) {
        return UITableViewCellEditingStyleInsert;
    }
    else {
        return UITableViewCellEditingStyleDelete;
    }
}

#pragma mark - 主要
// 删除对应数据
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [self.tableArray removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
